﻿#include "setup6wind.h"
#include "ui_setup6wind.h"
#include <mainwindow.h>
extern intListener doorStatus;
setup6wind::setup6wind(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::setup6wind)
{
    ui->setupUi(this);
    connect(&doorStatus, &intListener::valueChanged,this,&setup6wind::doorStatusChanged,Qt::QueuedConnection);

    ui->progressBar->setRange(0,100); //设置进度条最小值和最大值(取值范围)
    ui->progressBar->setMinimum(0); //设置进度条最小值

    ui->progressBar->reset();
#ifdef Q_OS_ANDROID
    int fontId = QFontDatabase::addApplicationFont(":/FontStyle/AlibabaPuHuiTi-3-55-Regular.ttf");
    if (fontId != -1) {
        QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId);
        if (!fontFamilies.isEmpty()) {
            QString familyName = fontFamilies.first();
            QFont font(familyName, 18); // 创建一个QFont对象，设置字体名称和默认大小

            // 使用setFont方法设置字体，传入QFont对象
            ui->label->setFont(font);
            ui->label_2->setFont(font);
            ui->comboBox->setFont(font);
            ui->progressBar->setFont(font);
            ui->SaveBtn_3->setFont(font);
            ui->SaveBtn_2->setFont(font);
            ui->SaveBtn->setFont(font);


            // 如果需要为其他按钮设置不同的大小，可以创建新的QFont对象
            QFont smallerFont(familyName, 16);
            ui->returnBtn->setFont(familyName);
            ui->pushButton_9->setFont(familyName);
        }
    }
#endif
}

void setup6wind::doorStatusChanged(){
    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setText("        打开仓门");
    }
    else
    {
        ui->pushButton_9->setText("        关闭仓门");
    }
}
setup6wind::~setup6wind()
{
    delete ui;
}

void setup6wind::showEvent(QShowEvent *event)
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select ExperimentName from ExperimentInfo";
    query.exec(sql);
    while (query.next()) {
        syname=query.value("ExperimentName").toString();
    }
    ui->comboBox->clear();
    ui->comboBox->addItem(syname);
    tcpClient = TcpClient::getInstance();
    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setText("        打开仓门");
    }
    else
    {
        ui->pushButton_9->setText("        关闭仓门");
    }
    event->accept();
}

void setup6wind::on_returnBtn_clicked()
{
    this->close();
    emit changeTotalWindow();
}

void setup6wind::on_SaveBtn_2_clicked()
{
    tcts1 = new tcts();
    tcts1->infochange("提示","info","是否取消恢复?",true,true);
    // 获取屏幕的中心点
    QRect screenGeometry = QApplication::desktop()->screenGeometry();
    int centerX = screenGeometry.center().x();
    int centerY = screenGeometry.center().y();
    int labelWidth = tcts1->sizeHint().width(); // 获取QLabel的建议大小
    int labelHeight = tcts1->sizeHint().height();
    int labelX = centerX-labelWidth/2;
    int labelY = centerY-labelHeight/2;
    tcts1->show();
    tcts1->move(labelX, labelY);
    connect(tcts1->ui->pushButton_2, &QPushButton::clicked,[this](){
        ui->progressBar->setValue(0);
        syhftime=1;
        disconnect(timSend,0,0,0);
    });
}

void setup6wind::on_pushButton_9_clicked()
{
    if(doorStatus.get()==0)
    {
        doorStatus.set(1);
        ui->pushButton_9->setText("        关闭仓门");
    }
    else
    {
        doorStatus.set(0);
        ui->pushButton_9->setText("        打开仓门");
    }
}

void setup6wind::on_SaveBtn_clicked()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql="delete from FluorescenceData ;";
    query.exec(sql);

    sql="Select CycleNum from AmplStep where ProgramId=(Select AmplGuid from ExperimentInfo) and "
        "CASE WHEN "
        "(Select count(*) from AmplStep where ProgramId=(Select AmplGuid from ExperimentInfo) and IsCollect='是')>0 "
        "then "
        "IsCollect='是' "
        "else "
        "StepNum=(Select max(StepNum) from AmplStep where programId=(Select AmplGuid from ExperimentInfo)) "
        "end";
    query.exec(sql);
    while (query.next()) {
        xhval=query.value("CycleNum").toInt();
    }
    qDebug()<<xhval;
    ui->progressBar->setMaximum(xhval); //设置进度条最大值
    timSend->setInterval(4000);
    connect(timSend,&QTimer::timeout,this,[=]()
    {
        if(syhftime<=xhval){
            QString type = "0007";
            QString type2 = QString("%1").arg(syhftime, 4, 16, QLatin1Char('0'));
            QString end2 = "0000";
            int len = 10;
            QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
            QString data = "6200003C"+len1+type+end2+type2+end2+"55AA";
            QString finalData;
            for (int i = 0; i < data.length(); ++i) {
                finalData.append(data.at(i));
                // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
                if ((i+1)%2==0) {
                    finalData.append(' ');
                }
            }
            QByteArray finalInfo=common->GetSendData(finalData);
            tcpClient->ClientSendingData(finalInfo);
            ui->progressBar->setValue(syhftime);  //设置当前的运行值
            qDebug()<<xhval<<syhftime;
            syhftime++;
        }else if(syhftime>xhval){
            tcts1 = new tcts();
            tcts1->infochange("提示","treat","恢复成功",false,true);
            // 获取屏幕的中心点
            QRect screenGeometry = QApplication::desktop()->screenGeometry();
            int centerX = screenGeometry.center().x();
            int centerY = screenGeometry.center().y();
            int labelWidth = tcts1->sizeHint().width(); // 获取QLabel的建议大小
            int labelHeight = tcts1->sizeHint().height();
            int labelX = centerX-labelWidth/2;
            int labelY = centerY-labelHeight/2;
            tcts1->show();
            tcts1->move(labelX, labelY);
            ui->progressBar->reset();
            syhftime=1;
            ui->progressBar->setValue(0);
            disconnect(timSend,0,0,0);
            this->hide();
            emit changeAnalyWindow();
        }
    });
    timSend->start();
}
